<template>
  <div class="app-container" style="font-size: 14px;font-weight: 500">
    <!-- $t is vue-i18n global function to translate lang -->

    <el-button type="primary" size="small" @click="hrefTo">
      <svg-icon icon-class="add_table" class-name="card-panel-icon icon-refresh"/>
    </el-button>
    <el-button type="primary" size="small" style="margin-bottom:20px" @click="getList">
      <svg-icon icon-class="refresh" class-name="card-panel-icon icon-refresh"/>
    </el-button>
    <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :element-loading-text="$t('table.load')"
        :data="list"
        stripe
        fit
        size="medium"
        highlight-current-row
        @selection-change="handleSelectionChange"
    >

      <el-table-column type="selection" align="center"/>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('users.name')">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('users.account')">
        <template slot-scope="scope">
          {{ scope.row.account }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('users.role')">
        <template slot-scope="scope">
          {{ scope.row.role }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('users.money')">
        <template slot-scope="scope">
          {{ scope.row.money }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('users.phone')">
        <template slot-scope="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('users.email')">
        <template slot-scope="scope">
          {{ scope.row.email }}
        </template>
      </el-table-column>
      <el-table-column
          fixed="right"
          :label="$t('table.actions')"
          width="100"
      >
        <template slot-scope="{row,$index}">
          <el-button type="text" size="small" @click="handleUpdate(row,$index)">{{ $t('users.recharge') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="getList"/>
    <el-dialog :title="$t('users.recharge')" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="rechargeFrom" label-position="left" label-width="70px"
               style="width: 400px; margin-left:50px;">
        <el-form-item label="积分余额" prop="title">
          <el-button>{{ number }}</el-button>
        </el-form-item>

        <el-form-item :label="$t('users.recharge')" prop="title">
          <el-input-number v-model="rechargeFrom.money" :min="minMoney"  :max="maxMoney" :step="10000"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          {{ $t('table.cancel') }}
        </el-button>
        <el-button type="primary" :loading="loading" @click="onRecharge">
          {{ $t('table.confirm') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {list, recharge} from '@/api/user'
import Pagination from '@/components/Pagination'
import store from '@/store'
import {mapGetters} from "vuex";

export default {
  name: 'SelectExcel',
  inject: ['reload'],
  components: {Pagination},
  data() {
    return {
      list: null,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      listLoading: true,
      multipleSelection: [],
      downloadLoading: false,
      loading: false,
      dialogFormVisible: false,
      rechargeFrom: {
        id: 0,
        money: 1
      },
      minMoney: 100,
      maxMoney: 'Infinity',
      rules: {
        money: [{required: true, message: 'type is required', trigger: 'change'}]
      }
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'avatar',
      'account',
      'number',
      'useNumber',
      'role'
    ])
  },
  created() {
    this.getList()
  },
  methods: {
    onRecharge() {
      this.$refs.dataForm.validate(valid => {
        if (!valid) {
          return false
        }

        this.loading = true
        recharge({
          userId: this.rechargeFrom.id,
          money: this.rechargeFrom.money
        }).then(response => {
          this.$confirm('recharged successfully！', 'title', {
            confirmButtonText: 'confirm',
            showCancelButton: false,
            type: 'success'
          }).then(() => {
            this.reload()
          })
          this.loading = false
          this.dialogFormVisible = false
          // this.$store.state.number = response.data.integral
          this.$store.dispatch('user/getInfo','number')
          this.reload()
        }).catch(() => {
          this.loading = false
        })
      })
    },
    handleUpdate(row, index) {
      const temp = Object.assign({}, row) // copy obj
      this.rechargeFrom.id = temp.id
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    getList() {
      this.listLoading = true
      list({
        page: this.listQuery.page,
        limit: this.listQuery.limit
      }).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    hrefTo() {
      this.$router.push('/users/create')
    }
  }
}
</script>
<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 120px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 28%;
}

.icon-refresh {
  color: #ffffff;
}
</style>
